const resultEl = document.getElementById("result");
const lengthEl = document.getElementById("passwordLength");
const uppercaseEl = document.getElementById("uppercase");
const lowercaseEl = document.getElementById("lowercase");
const numbersEl = document.getElementById("numbers");
const symbolsEl = document.getElementById("symbols");
const generateEl = document.getElementById("generate");

generateEl.addEventListener("click", () => {
  const length = +lengthEl.value; // 获取密码长度
  const hasLower = lowercaseEl.checked; // 获取包含小写是否选中
  const hasUpper = uppercaseEl.checked; // 获取包含大写是否选中
  const hasNumber = numbersEl.checked; // 获取包含数字是否选择
  const hasSymbol = symbolsEl.checked; // 获取包含特殊字母是否选中
  // 将随机生成的密码显示到 input 框中
  resultEl.value =
    generatePassword(hasLower, hasUpper, hasNumber, hasSymbol, length) ?? "";
});

/**
 * @function_name generatePassword ->生成密码的函数
 * @param {*} lower 是否小写
 * @param {*} upper 是否大写
 * @param {*} number 是否是数字
 * @param {*} symbol 是否是特殊符号
 * @param {*} length 密码长度
 * @return {*} string
 */
const lowerArr = [..."abcdefghijklmnopqrstuvwxyz"];
const upperArr = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
const numberArr = [..."1234567890"];
const symbolArr = [..."!@#$%^&*(){}[]=<>/,."];
function generatePassword(lower, upper, number, symbol, length) {
  if (!(lower || upper || number || symbol)) {
    // console.log('no');
    return "";
  }
  //TODO：待补充代码
  let arr = "";
  // console.log(arr.length != length);
  if (length == 4) {
    let arr1 = [];
    if (lower) arr1.push(0);
    if (upper) arr1.push(1);
    if (number) arr1.push(2);
    if (symbol) arr1.push(3);
    let oldArr1 = [...arr1];
    // for (let i = 0; i < array.length; i++) {
    while (arr.length != length) {
      let ran2 = Math.floor(Math.random() * arr1.length);
      switch (arr1[ran2]) {
        case 0:
          arr = lowerFn(arr, lower);
          arr1.splice(ran2, 1);
          break;
        case 1:
          arr = upperFn(arr, upper);
          arr1.splice(ran2, 1);
          break;
        case 2:
          arr = numberFn(arr, number);
          arr1.splice(ran2, 1);
          break;
        case 3:
          arr = symbolFn(arr, symbol);
          arr1.splice(ran2, 1);
          break;
      }
      if (arr1.length == 0) {
        arr1 = [...oldArr1];
      }
    }
    // }
  }
  while (arr.length != length && length > 4) {
    let ran1 = Math.floor(Math.random() * 4);
    switch (ran1) {
      case 0:
        arr = lowerFn(arr, lower);
        break;
      case 1:
        arr = upperFn(arr, upper);
        break;
      case 2:
        arr = numberFn(arr, number);
        break;
      case 3:
        arr = symbolFn(arr, symbol);
        break;
    }
  }
  return arr;
}
function lowerFn(arr, isHas) {
  if (!isHas) return arr;
  let ran = Math.floor(Math.random() * lowerArr.length);
  arr += lowerArr[ran];
  return arr;
}
function upperFn(arr, isHas) {
  if (!isHas) return arr;
  let ran = Math.floor(Math.random() * upperArr.length);
  arr += upperArr[ran];
  return arr;
}
function numberFn(arr, isHas) {
  if (!isHas) return arr;
  let ran = Math.floor(Math.random() * numberArr.length);
  arr += numberArr[ran];
  return arr;
}
function symbolFn(arr, isHas) {
  if (!isHas) return arr;
  let ran = Math.floor(Math.random() * symbolArr.length);
  arr += symbolArr[ran];
  return arr;
}
